from pandac.PandaModules import *
import direct.directbase.DirectStart
from direct.showbase.DirectObject import DirectObject
from direct.task import Task
import sys

class World(DirectObject):
    def __init__(self):

		base.enableParticles()

		# Set 'Escape' as the the close game button.
		self.accept("escape", sys.exit)

		#Load pipe model
		self.pipe = loader.loadModel("MainTube")
		self.pipe.setScale(0.2, 0.2, 0.2)
                self.pipe.setPos(-5, 0, -5)
                self.pipe.setR(35)
                self.pipe.setTransparency(TransparencyAttrib.MDual)
                self.pipe.setColor(1,1,0,0.5)
		self.pipe.setCollideMask(BitMask32.allOn())

		self.gravityFN = ForceNode('world-forces')
		self.gravityFNP = render.attachNewNode(self.gravityFN)
		self.gravityForce = LinearVectorForce(0,0,-9.81) #gravity acceleration
		self.gravityFN.addForce(self.gravityForce)
		base.physicsMgr.addLinearForce(self.gravityForce)

		# Load the sphere model.
		self.sphere = loader.loadModel("ball")
                self.sphere.reparentTo(render)
                self.sphere.setCollideMask(BitMask32.allOff())

		self.Node = NodePath(PandaNode("PhysicsNode"))
                self.Node.reparentTo(render)
		self.an = ActorNode("sphere-physics")
		self.anp = self.Node.attachNewNode(self.an)
                self.anp.setPos(0, 0, 40)

	
		self.an.getPhysicsObject().setVelocity(0, 0, 0)

                self.collider = self.anp.attachNewNode(CollisionNode('projectilcnode'))
                self.collider.node().addSolid(CollisionSphere(0, 0, 0, 0.5))
                self.collider.node().setIntoCollideMask(BitMask32.allOff())

		self.an.getPhysicsObject().setMass(30)
                base.physicsMgr.attachPhysicalNode(self.an)

		# Reparent the model to render.
                
		self.Node.reparentTo(render)
		self.sphere.reparentTo(self.anp)
		self.pipe.reparentTo(render)

		# Initialize the collision traverser.
		base.cTrav = CollisionTraverser()

		# Initialize the Pusher collision handler.

		self.pusher = PhysicsCollisionHandler()
                self.pusher.setHorizontal(False)
                self.pusher.setDynamicFrictionCoef(1.0) 
                self.pusher.setStaticFrictionCoef(1.0) 

                self.pusher.addCollider(self.collider, self.anp)
		base.cTrav.addCollider(self.collider, self.pusher)
                base.cTrav.setRespectPrevTransform(True)

		#self.collider.show()
		#base.cTrav.showCollisions(render)

		self.Node.reparentTo(render)

                dlight = DirectionalLight('dlight')
                dlight.setColor(VBase4(1, 1, 1, 1)) # white light
                dlnp = render.attachNewNode(dlight)
                dlnp.setPos(50, 50, 20)
                dlnp.lookAt(0, 0, 0)
                render.setLight(dlnp)

		# Set the camera position
		base.disableMouse()
		base.camera.setPos(0, 30, -20)
		base.camera.lookAt(0, 0, -0)

    # The task for our simulation
    def simulationTask(self, task):    
        return task.cont

w = World()
run()
